-- ======================================================================
-- Hell Lua Bus
-- 
-- Copyright 2012-2013Hell-Prototypes
--
-- http://www.hellprototypes.com/
--
-- This is free software, licensed under the terms of the GNU General
-- Public License as published by the Free Software Foundation.
-- ======================================================================

hlb = require('libhlb')

--[[
===+=========+============================
ENA| IN1 IN2 | OUT1 OUT2 OUT3 OUT4 Note
===+=========|============================
 L |  — —  | OFF  OFF  OFF  OFF  Standby
-------------+---------------------------
   | L   L   | H    L    H    L
 H | L   H   | H    L    L    H     2-phase excitation
   | H   H   | L    H    L    H
   | H   L   | L    H    H    L
===+=========+============================
--]]

function device_open()
	local ret = hlb.usb_open()
	if ret < 0 then
		print("Fail to open usb device")
		return false
	end
	
	return true
end

function device_close()
	local ret = hlb.usb_close()
	if ret < 0 then
		print("Fail to close usb device")
		return false
	end
	
	return true
end

function pin_set(pin)
	local ret = hlb.set_bit( pin )
	if ret < 0 then
		print("Pin" .. pin .." : hlb_set_bit ret = ", ret)
	end
end

function pin_reset( pin )
	local ret = hlb.clr_bit( pin )
	if ret < 0 then
		print("Pin" .. pin .." : hlb_clr_bit ret = ", ret)
	end
end

PIN_ENA = 2
PIN_IN11 = 0
PIN_IN12 = 1

PIN_IN21 = 3
PIN_IN22 = 4
-- -----------------------------------------------
if not device_open() then
	return
end

ret = hlb.port_write(0x00)
print("ret0 = " .. ret)
if ret < 0 then
	return
end 

ret = hlb.port_oe(0x0f)
print("ret1 = " .. ret)
if ret < 0 then
	return
end 

delay_time = 1
function step1_cw(step_num)
    if step_num == nil then
        return
    end
    pin_set(PIN_ENA)
    for i=1, step_num do
        hlb.msdelay(delay_time)
        pin_set(PIN_IN12)
        hlb.msdelay(delay_time)
        pin_set(PIN_IN11)
        hlb.msdelay(delay_time)
        pin_reset(PIN_IN12)
        hlb.msdelay(delay_time)
        pin_reset(PIN_IN11)
    end
    pin_reset(PIN_ENA)
end

function step1_ccw(step_num)
    if step_num == nil then
        return
    end
    pin_set(PIN_ENA)
    for i=1, step_num do
        --print("i = " .. i)
        hlb.msdelay(delay_time)
        pin_set(PIN_IN11)
        hlb.msdelay(delay_time)
        pin_set(PIN_IN12)
        hlb.msdelay(delay_time)
        pin_reset(PIN_IN11)
        hlb.msdelay(delay_time)
        pin_reset(PIN_IN12)
    end
    pin_reset(PIN_ENA)
end

-- -----------------------------------------------------------------------------
function step2_cw(step_num)
    if step_num == nil then
        return
    end
    pin_set(PIN_ENA)
    for i=1, step_num do
        hlb.msdelay(delay_time)
        pin_set(PIN_IN22)
        pin_set(PIN_IN12)
        hlb.msdelay(delay_time)
        pin_set(PIN_IN21)
        pin_set(PIN_IN11)
        hlb.msdelay(delay_time)
        pin_reset(PIN_IN22)
        pin_reset(PIN_IN12)
        hlb.msdelay(delay_time)
        pin_reset(PIN_IN21)
        pin_reset(PIN_IN11)
    end
    pin_reset(PIN_ENA)
end

function step2_ccw(step_num)
    if step_num == nil then
        return
    end
    pin_set(PIN_ENA)
    for i=1, step_num do
        --print("i = " .. i)
        hlb.msdelay(delay_time)
        pin_set(PIN_IN21)
        pin_set(PIN_IN11)
        hlb.msdelay(delay_time)
        pin_set(PIN_IN22)
        pin_set(PIN_IN12)
        hlb.msdelay(delay_time)
        pin_reset(PIN_IN21)
        pin_reset(PIN_IN11)
        hlb.msdelay(delay_time)
        pin_reset(PIN_IN22)
        pin_reset(PIN_IN12)
    end
    pin_reset(PIN_ENA)
end

while true do
	print("")
	print("===== Lead Screw Menu ======")
	print("= 1. step 1 cw             =")
	print("= 2. step 1 ccw            =")
    print("= 3. move 1 test           =")
	print("= 4. step 2 cw             =")
	print("= 5. step 2 ccw            =")
    print("= 6. move 2 test           =")
    print("= a b c d . step test      =")
	print("= Q. Exit                  =")
	print("============================")
	io.write("> Select Operation: ")
	io.flush()
	local input = io.read()
	print("")
    
    if input == "1" then
        step1_cw(4)
    elseif input == "2" then
        step1_ccw(4)
    elseif input == "3" then
        for i=1, 4 do
            print("i = " .. i)
            step1_cw(90)
            hlb.msdelay(50)
            step1_ccw(90)
            hlb.msdelay(50)
        end
    elseif input == "4" then
        step2_cw(4)
    elseif input == "5" then
        step2_ccw(4)
    elseif input == "6" then
        for i=1, 2 do
            print("i = " .. i)
            step2_cw(90)
            hlb.msdelay(50)
            step2_ccw(90)
            hlb.msdelay(50)
        end
    elseif input == "a" then
        pin_set(PIN_ENA)
            pin_set(PIN_IN22)
        pin_reset(PIN_ENA)
    elseif input == "b" then
        pin_set(PIN_ENA)
            pin_set(PIN_IN21)
        pin_reset(PIN_ENA)
    elseif input == "c" then
        pin_set(PIN_ENA)
            pin_reset(PIN_IN22)
        pin_reset(PIN_ENA)
    elseif input == "d" then
        pin_set(PIN_ENA)
            pin_reset(PIN_IN21)
        pin_reset(PIN_ENA)
    elseif input == "Q" or input == "q" then
		break
	else
		print("> Unsupport Key: " .. input)
	end
end

ret = hlb.port_oe(0x00)
print("ret2 = " .. ret)

device_close()
